const { verifyToken } = require('../../utils/jwtUtils');

const verifyAuthToken = async (req,res,next) => {
  const authHeader = req.headers.authorization;
  if (!authHeader || !authHeader.startsWith('Bearer ')) {
    const err = new Error(`token不能为空`);
    err.statusCode = 401; // 设置状态码
    next(err); // 传递给错误处理中间件
  }
  
  const token = authHeader.split(' ')[1];
  
  const decoded = verifyToken(token);
  if(!decoded){
    const err = new Error(`token无效`);
    err.statusCode = 401; // 设置状态码
    next(err); // 传递给错误处理中间件
  }
  next();
}

module.exports = {
  verifyAuthToken,
}